home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj1191.zip / NETPRINT.ASC < prev    next >
Text File  |  1991-10-18  |  10KB  |  393 lines

  1. _MONITORING DISTRIBUTED PRITNERS UNDER NOVELL NETWARE_
  2. by V. James Krammes
  3.  
  4. [LISTING ONE]
  5.  
  6.  
  7. #include <dos.h>
  8. extern char far *screen;
  9. void CursorOff()
  10. {
  11.     union REGS regs;
  12.     regs.h.ah = 1;
  13.     regs.h.ch = 0x20;
  14.     regs.h.cl = 0;
  15.     int86(0x10,®s,®s);
  16. }
  17.  
  18. void CursorOn()
  19. {
  20.     union REGS regs;
  21.     regs.h.ah = 1;
  22.     regs.h.ch = 11;
  23.     regs.h.cl = 12;
  24.     int86(0x10,®s,®s);
  25. }
  26.  
  27. void CursorAt(r,c)
  28.     unsigned char r,c;
  29. {
  30.     union REGS regs;
  31.     regs.h.ah = 02;
  32.     regs.h.bh = 0;
  33.     regs.h.dh = r;
  34.     regs.h.dl = c;
  35.     int86(0x10,®s,®s);
  36. }
  37.  
  38. void _cls(attr)
  39.     unsigned char attr;
  40. {
  41.     union REGS regs;
  42.     regs.h.ah = 0x06;                       /* scroll window up */
  43.     regs.h.al = 0;                          /* clear entire window */
  44.     regs.h.bh = attr;
  45.     regs.x.cx = 0;                          /* upper left = (0,0) */
  46.     regs.h.dh = 23;
  47.     regs.h.dl = 79;
  48.     int86(0x10,®s,®s);
  49. }
  50.  
  51. void clearline(row,attr)
  52.     unsigned char row,attr;
  53. {
  54.     union REGS regs;
  55.     regs.h.ah = 0x06;                       /* scroll window up */
  56.     regs.h.al = 1;                          /* clear 1 line */
  57.     regs.h.bh = attr;
  58.     regs.h.ch = row;                        /* upper left = (row,0) */
  59.     regs.h.cl = 0;
  60.     regs.h.dh = row;                        /* lower right = (row,79) */
  61.     regs.h.dl = 79;
  62.     int86(0x10,®s,®s);
  63. }
  64.  
  65. void PutStrNoAttr(row,col,str,len)
  66.     unsigned char row,col;
  67.     int len;
  68.     char *str;
  69. {
  70.     register int offset = (row * 160) + (col << 1);
  71.     while (len--) {
  72.         *(screen + offset) = *str++;
  73.         offset += 2;
  74.     }
  75. }
  76.  
  77. void putattr(row,col,attr,cnt)
  78.     unsigned char row,col,attr;
  79.     int cnt;
  80. {
  81.     register int offset = (row * 160) + (col << 1) + 1;
  82.     while (cnt--) {
  83.         *(screen + offset) = attr;
  84.         offset += 2;
  85.     }
  86. }
  87.  
  88.  
  89. [LISTING TWO]
  90.  
  91. void CursorOff(void);
  92. void CursorOn(void);
  93. void CursorAt(unsigned char,unsigned char);
  94. void _cls(unsigned char);
  95. void clearline(unsigned char,unsigned char);
  96. void PutStrNoAttr(unsigned char,unsigned char,char *,int);
  97. void putattr(unsigned char,unsigned char,unsigned char,int);
  98.  
  99.  
  100. [LISTING THREE]
  101.  
  102. /* PMON.H - header file for PMON: printer monitor. */
  103.  
  104. /** define structures **/
  105. typedef struct _Printer {
  106.             BYTE        Printer;
  107.             int     Status;
  108.             BYTE        Problem;
  109.             BYTE        HasJob;
  110.             WORD        Copies;
  111.             WORD        CopiesDone;
  112.             long        JobSize;
  113.             long        BytesDone;
  114.             float       PercentDone;
  115.             struct _Printer *next;
  116. } Printer;
  117.  
  118. typedef struct _PrintServer {
  119.             WORD        ConnectionID;
  120.             char        Name[48];
  121.             WORD        SPXConnection;
  122.             int     Error;
  123.             Printer     *Printers;
  124. } PrintServer;
  125.  
  126.  
  127. [LISTING FOUR]
  128.  
  129. #include <stdio.h>
  130. #include <stdlib.h>
  131. #include <dos.h>
  132. #include <nit.h>
  133. #include <npt.h>
  134. #include <string.h>
  135. #include "pmon.h"
  136. #include "intrface.h"
  137.  
  138. char ConfigFileName[128];
  139. FILE *ConfigFile;
  140.  
  141. unsigned DelayValue = 500;
  142.  
  143. PrintServer PS[8];
  144. Printer     *P;
  145.  
  146. char far *screen;
  147.  
  148. void OpenConfigFile(void)
  149. {
  150.     ConfigFile = fopen(ConfigFileName,"r");
  151.     if (!ConfigFile) {
  152.            printf("Unable to open configuration file \"%s\"\n",ConfigFileName);
  153.         exit(1);
  154.     }
  155. }
  156.  
  157. void strrep(char *s,char c1,char c2)
  158. {
  159.     while (*s) {
  160.         if (*s == c1)
  161.             *s = c2;
  162.         s++;
  163.     }
  164. }
  165.  
  166. void AddPrinterToServer(int psnum,BYTE prnum)
  167. {
  168.     register Printer *p = PS[psnum].Printers;
  169.     if (!p) {
  170.         PS[psnum].Printers = calloc(1,sizeof(Printer));
  171.         PS[psnum].Printers->Printer = prnum;
  172.     } else {
  173.         while (p->next)
  174.             p = p->next;
  175.         p->next = calloc(1,sizeof(Printer));
  176.         p = p->next;
  177.         p->Printer = prnum;
  178.     }
  179. }
  180.  
  181. void BuildServerList(void)
  182. {
  183.     char buf[256];
  184.     char *ptr;
  185.     int status;
  186.     WORD ConnectionID;
  187.     fgets(buf,255,ConfigFile);
  188.     while (strlen(buf)) {
  189.         strrep(buf,'\n',0);
  190.         if (buf[0] != ';' && strlen(buf)) {
  191.             ptr = strtok(buf,"/");
  192.             if (!ptr || strlen(ptr) > 47) {
  193.                           printf("Error: expected <printserver>/<printer#>\n");
  194.               printf("       found \"%s\"\n",buf);
  195.               exit(1);
  196.             }
  197.             status = GetConnectionID(ptr,&ConnectionID);
  198.             if (status) {
  199.          printf("Error: You are not logged in to server \"%s\"\n",ptr);
  200.                 exit(1);
  201.             }
  202.             if (!PS[ConnectionID-1].ConnectionID) {
  203.                 PS[ConnectionID-1].ConnectionID = ConnectionID;
  204.                 strcpy(PS[ConnectionID-1].Name,ptr);
  205.             }
  206.             ptr = strtok(NULL," \n\t;");
  207.             if (!ptr || !isdigit(*ptr)) {
  208.               printf("Error: expected <printserver>/<printer#>\n");
  209.               printf("       found \"%s\"\n",buf);
  210.               exit(1);
  211.             }
  212.             AddPrinterToServer(ConnectionID-1,atoi(ptr));
  213.         }
  214.         fgets(buf,255,ConfigFile);
  215.     }
  216. }
  217.  
  218. void Initialize()
  219. {
  220.     register int i;
  221.   printf("PMON 1.00 - (C) 1991 The Midland Mutual Life Insurance Company\n\n");
  222.     screen = (char far *) MK_FP(0xB800,0x0000);
  223.     for (i=0; i < 8; i++)
  224.         memset((char *) &PS[i],0,sizeof(PrintServer));
  225.     OpenConfigFile();
  226.     BuildServerList();
  227.     fclose(ConfigFile);
  228. }
  229.  
  230. void ScreenSetup(void)
  231. {
  232.     static char *title =
  233.         "(C) 1991 The Midland Mutual Life Insurance Company";
  234.     static char *header =
  235.         "  Server  /P#   Status   #Copies   Size of 1   Done So Far  Percent";
  236.     static char *footer =
  237.         "Press any Key to Exit";
  238.     _cls(0x1F);
  239.     PutStrNoAttr(0,(80-strlen(title)) >> 1,title,strlen(title));
  240.     PutStrNoAttr(2,6,header,strlen(header));
  241.     PutStrNoAttr(23,(80-strlen(footer)) >> 1,footer,strlen(footer));
  242. }
  243.  
  244. void LoginToServer(int i)
  245. {
  246.     register int status;
  247.     BYTE CAL;
  248.     if (PS[i].ConnectionID) {
  249.         SetPreferredConnectionID(PS[i].ConnectionID);
  250.                status = PSAttachToPrintServer(PS[i].Name,&PS[i].SPXConnection);
  251.         if (status)
  252.             PS[i].Error = status;
  253.         else {
  254.                status = PSLoginToPrintServer(PS[i].SPXConnection,&CAL);
  255.             if (status)
  256.                 PS[i].Error = status;
  257.         }
  258.     }
  259. }
  260.  
  261. void DetachFromServer(int i)
  262. {
  263.     if (PS[i].ConnectionID)
  264.         PSDetachFromPrintServer(PS[i].SPXConnection);
  265. }
  266.  
  267. void GetPrinterInformation(WORD spxid,Printer *p)
  268. {
  269.     char sdummy[128];
  270.     BYTE dummy;
  271.     WORD wdummy;
  272.     register int status;
  273.     status = PSGetPrinterStatus(spxid,p->Printer,&dummy,&(p->Problem),
  274.         &(p->HasJob),&dummy,&wdummy,sdummy,sdummy);
  275.     p->Status = status;
  276.     if (p->HasJob) {
  277.         PSGetPrintJobStatus(spxid,p->Printer,sdummy,sdummy,
  278.             &wdummy,sdummy,&(p->Copies),&(p->JobSize),
  279.             &(p->CopiesDone),&(p->BytesDone),&wdummy,
  280.             &dummy);
  281.         if (p->JobSize == 0.0 || p->BytesDone == 0.0)
  282.             p->PercentDone = 0.0;
  283.         else
  284.         p->PercentDone = (float) (p->BytesDone) / (float) (p->JobSize) * 100.0;
  285.     }
  286. }
  287. char *TroubleDescription(BYTE code)
  288. {
  289.     static char *Desc[] = { "   OK   ",
  290.                 " JAMMED ",
  291.                 "No Paper" };
  292.     if (code == 1)
  293.         return Desc[1];
  294.     else if (code == 2)
  295.         return Desc[2];
  296.     else
  297.         return Desc[0];
  298. }
  299. char *CommaString(long num)
  300. {
  301.     static char buf2[15];
  302.     char buf1[15];
  303.     register int p1,p2;
  304.     int positions = 0;
  305.     memset(buf1,0,15);
  306.     memset(buf2,0,15);
  307.     sprintf(buf1,"%ld",num);
  308.     p1 = strlen(buf1);
  309.     p2 = (p1 > 9) ? p1 + 3 : (p1 > 6) ? p1 + 2 : (p1 > 3) ? p1 + 1 : p1;
  310.     p1--;
  311.     p2--;
  312.     while (p1 >= 0) {
  313.         buf2[p2--] = buf1[p1--];
  314.         if (++positions % 3 == 0)
  315.             buf2[p2--] = ',';
  316.     }
  317.     return buf2;
  318. }
  319.  
  320. void DisplayPrinterInformation(PrintServer *ps,Printer *p,int row)
  321. {
  322.     char buf[80],s1[15],s2[15];
  323.     if (row <= 21) {
  324.         if (ps->Error)
  325.             sprintf(buf,"%-10s/%02d  Error #%d",ps->Name,
  326.                 p->Printer,ps->Error);
  327.         else if (p->Status)
  328.             sprintf(buf,"%-10s/%02d  Error #%d",ps->Name,
  329.                 p->Printer,p->Status);
  330.         else if (!p->HasJob)
  331.             sprintf(buf,"%-10s/%02d  %-8s",ps->Name,p->Printer,
  332.                 TroubleDescription(p->Problem));
  333.         else {
  334.             strcpy(s1,CommaString(p->JobSize));
  335.             strcpy(s2,CommaString(p->BytesDone));
  336.                 sprintf(buf,"%-10s/%02d  %-8s   %02d/%02d   %11s  %11s  %5.2f",
  337.         ps->Name,p->Printer,TroubleDescription(p->Problem),
  338.                 p->CopiesDone,p->Copies,s1,s2,p->PercentDone);
  339.         }
  340.         clearline(row,0x1f);
  341.         PutStrNoAttr(row,6,buf,strlen(buf));
  342.         if (p->Problem == 1)
  343.             putattr(row,0,0x9C,80);
  344.         else if (p->Problem == 2)
  345.             putattr(row,00,0x9E,80);
  346.     }
  347. }
  348.  
  349. void MainLoop(void)
  350. {
  351.     register Printer *p;
  352.     register int i;
  353.     int CurrentRow = 4;
  354.     for (i=0; i<8; i++)
  355.         if (PS[i].ConnectionID) {
  356.             LoginToServer(i);
  357.             p = PS[i].Printers;
  358.             while (p) {
  359.                   GetPrinterInformation(PS[i].SPXConnection,p);
  360.                   DisplayPrinterInformation(&PS[i],p,CurrentRow++);
  361.                 p = p->next;
  362.             }
  363.             DetachFromServer(i);
  364.         }
  365. }
  366.  
  367. main(int argc,char *argv[])
  368. {
  369.     if (argc == 1)
  370.         strcpy(ConfigFileName,"PMON.CFG");
  371.     else if (argc == 2)
  372.         strcpy(ConfigFileName,argv[1]);
  373.     else {
  374.         printf("Usage - PMON [<config-file>]\n");
  375.         exit(1);
  376.     }
  377.     Initialize();
  378.     CursorOff();
  379.     ScreenSetup();
  380.     while (!kbhit()) {
  381.         MainLoop();
  382.         delay(DelayValue);
  383.     }
  384.     CursorOn();
  385.     if (!getch())
  386.         getch();
  387.     CursorAt(24,0);
  388.     return 0;
  389. }
  390.  
  391.  
  392.  
  393.